package com.woniuxy.main.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Shared;
import com.woniuxy.main.service.SharedService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Data
@Controller
@RequestMapping("/share")
@Scope("prototype")
public class SharedHandler {

	@Autowired
	private SharedService sharedService;
	
	//封装合并排序功能
	private List<Shared> mergeAndSort(List<Shared> listc,List<Shared> lists){
		//合并进TreeMap//排序
		Map<String, Shared> map = new TreeMap<>();
		for (Shared c : listc) {
			if (c!=null) {
				map.put(c.getCreateDate(), c);
			}
		}
		for (Shared s : lists) {
			if (s!=null) {
				map.put(s.getCreateDate(), s);
			}
		}
		//转为List
		List<Shared> result = new ArrayList<>();
		//遍历
		Set<Entry<String, Shared>> set = map.entrySet();
		Iterator<Entry<String, Shared>> iterator = set.iterator();
		while (iterator.hasNext()) {
			result.add(iterator.next().getValue());
		}
		//倒序
		Collections.reverse(result);		
		return result;
	}
	
	//全部动态
	@RequestMapping("/allShare")
	@ResponseBody
	public List<Shared> allShared(){
		//分别获取
		List<Shared> listc = sharedService.allSharedCoach();
		List<Shared> lists = sharedService.allSharedStudent();
		return mergeAndSort(listc,lists);
	}
	
	//我的关注
	@RequestMapping("/allFollow")
	@ResponseBody
	public List<Shared> allFollowed(Shared shared){
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		shared.setUid(currentUid);
		return mergeAndSort(sharedService.allFollowedCoach(shared),sharedService.allFollowedStudent(shared));
	}
	
	//相互关注
	@RequestMapping("/allFriend")
	@ResponseBody
	public List<Shared> allFriend(Shared shared){
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		shared.setUid(currentUid);	
		
		return mergeAndSort(sharedService.allFriendCoach(shared),sharedService.allFriendStudent(shared));
	}
	
	//自己可见
	@RequestMapping("/allCurrentUser")
	@ResponseBody
	public List<Shared> allCurrentUser(Shared shared){
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		shared.setUid(currentUid);
		
		List<Shared> list = sharedService.allCurrentUser(shared);
		Collections.reverse(list);
		return list;
	}
	
	//发布动态
	@RequestMapping("/publish")
	@ResponseBody
	public String publish(Shared shared){
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		shared.setUid(currentUid);
		
		String result = "发布失败";
		shared.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		if (sharedService.insertShared(shared)>0) {
			result = "发布成功";
		}
		return result;
	}
	
	//删除动态
	@RequestMapping("/deleteThis")
	@ResponseBody
	public String deleteThis(Shared shared){
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		shared.setUid(currentUid);
		
		String result = "删除失败";
		if (sharedService.deleteThis(shared)>0) {
			result = "删除成功";
		}
		return result;
	}
}
